package com.andbase.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import com.andbase.demo.R;
import com.andbase.demo.adapter.MenuRecyclerViewAdapter;
import com.andbase.demo.model.MenuItem;
import com.andbase.library.app.base.AbBaseActivity;
import com.andbase.library.utils.AbDialogUtil;
import com.andbase.library.view.recycler.AbRecyclerView;
import com.andbase.library.view.recycler.AbRecyclerViewLoadMoreListener;
import com.andbase.library.view.recycler.AbSpaceItemVerticalDecoration;
import com.andbase.qrcode.AbQRScanActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AbBaseActivity {

    private List<Object> itemList = null;
    private MenuRecyclerViewAdapter recyclerViewAdapter = null;
    private AbRecyclerView recyclerView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setToolbarView("AndBase Demo",false);

        itemList = new ArrayList<>();

        recyclerViewAdapter = new MenuRecyclerViewAdapter(this,itemList);

        recyclerView = (AbRecyclerView) this.findViewById(R.id.recycler_list);

        recyclerView.addItemDecoration(new AbSpaceItemVerticalDecoration(this,1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setRecyclerViewAdapter(recyclerViewAdapter);

        recyclerView.setRecyclerViewLoadMoreListener(new AbRecyclerViewLoadMoreListener() {
            @Override
            public void loadMore(int page) {
                loadData(page);
            }
        });

        loadData(1);
    }

    public void loadData(int page){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                itemList.clear();
                MenuItem menuItem = new MenuItem(1,"日历");
                menuItem.setDescription("可高度定制的日历，容易修改");
                itemList.add(menuItem);

                MenuItem menuItem2 = new MenuItem(2,"二维码识别");
                menuItem2.setDescription("识别率最高，最精简二维码识别");
                itemList.add(menuItem2);

                MenuItem menuItem3 = new MenuItem(3,"ScrollView+WebView");
                menuItem3.setDescription("ScrollView+WebView");
                itemList.add(menuItem3);

                recyclerViewAdapter.notifyDataSetChanged();

            }
        },200);

    }

    public void onItemClickListener(MenuItem menuItem){
        if(menuItem.getId() == 1){
            Intent intent  = new Intent(this,CalendarActivity.class);
            startActivity(intent);
        }else if(menuItem.getId() == 2){
            Intent intent = new Intent(this, AbQRScanActivity.class);
            intent.putExtra("cameraId", 0);
            intent.putExtra("orientation", 1);
            startActivityForResult(intent,AbQRScanActivity.REQUEST_CODE_QR);
        }else if(menuItem.getId() == 3){
            Intent intent = new Intent(this, ScrollViewNestedWebViewActivity.class);
            startActivity(intent);
        }else{
            Intent intent  = new Intent(this,WebViewActivity.class);
            intent.putExtra("url","file:///android_asset/index.html");
            intent.putExtra("title","");
            intent.putExtra("showRefresh",true);
            intent.putExtra("showToolbar",true);
            startActivity(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){
           if(requestCode == AbQRScanActivity.REQUEST_CODE_QR){
                Bundle bundle = data.getExtras();
                String qrcode = bundle.getString(AbQRScanActivity.RESULT);
                AbDialogUtil.showMessageDialog(MainActivity.this,"扫描结果", qrcode, "确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AbDialogUtil.removeDialog(MainActivity.this);
                    }
                });

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

